package com.wangsb;

import java.util.Scanner;

/**
 * @Description: TODO
 * @author: shenbing.wang@hand-china
 * @date: 2021年06月20日 13:07
 */
public class Code0302 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入第一個字符串：");
        String str1 = scanner.nextLine();
        System.out.println("請輸入第二個字符串：");
        String str2 = scanner.nextLine();
        boolean maxLength = str1.length() > str2.length();
        String substring = null;
        if (maxLength) {
            subString(str1, str2);
        } else {
            subString(str2, str1);
        }
    }

    private static void subString(String str1, String str2) {
        String substring;
        for (int i = 0; i < str2.length(); i++) {
            for (int j = 0; j <= i; j++) {
                substring = str2.substring(j, str2.length() - (i - j));
                if (str1.contains(substring)) {
                    System.out.println("最长子串是： " + substring);
                    return;
                }
            }
        }
    }
}
